package com.aaa.controller;

import com.aaa.pojo.*;
import com.aaa.service.FundType_service;
import com.aaa.service.Fund_Management_Service;
import com.aaa.service.GrowthRate_Service;
import com.aaa.service.InvestService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Management")
public class Fund_Management_Controller {
    @Autowired
    private Fund_Management_Service service;
    @Autowired
    private InvestService invest;
    @Autowired
    private FundType_service fundType;
    @Autowired
    private GrowthRate_Service growthRate;
    @RequestMapping("/Management")
    public String financial(){
        return
                "views/Fund_Management";
    }
    @RequestMapping(value = "/selectAll")
    public String selectAll(){
        Integer g_id;
        List<Fund_Management> fund_managements = service.selectAll();
        for (Fund_Management f: fund_managements) {
            g_id=f.g_id;
            System.out.println(g_id);
        }

        return "";
    }
            @ResponseBody
    @RequestMapping(value = "/getUserList/{currentPage}/{pageSize}",method = RequestMethod.GET)
   public PageInfo<Fund_Management> getUserByUserPage(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize){
        return service.getAllUsersByPage(currentPage,pageSize);
    }
    @RequestMapping(name = "添加",value = "/insert")
    public String insert(Integer type_code, String type_abbreviate, String type_name,String f_amount, double g_day,double g_month,double g_year, Integer h_id){
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = df.format(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_WEEK, h_id);
        String format1 = df.format(calendar.getTime());
        String s = format + "~" + format1;

        //添加
        invest.insertInvest(new Invest(s, h_id + ""));
        //查询
        Invest invest1=invest.selectDate(s);
        //H_id
        Integer h_id1 = invest1.h_id;

        //添加
        int min1 = 100000; // 定义随机数的最小值
        int max1 = 999999; // 定义随机数的最大值
        // 产生一个7~107的数
        int z1 = (int) min1 + (int) (Math.random() * (max1 - min1));
        fundType.insertFundType(new FundType(type_name,z1,type_abbreviate));
        //查询
        FundType fundTypeselect = this.fundType.selectCode(z1);
        //type_id
        Integer code = fundTypeselect.type_id;
        //添加
        Random r = new Random();
        int min = 7; // 定义随机数的最小值
        int max = 107; // 定义随机数的最大值
        // 产生一个7~107的数
        int z = (int) min + (int) (Math.random() * (max - min));
        growthRate.insertGrowthRate(new GrowthRate(z,g_day,g_month,g_year));
            System.out.println(z);
        service.insert(new Fund_Management(f_amount,z,h_id1,code));
        return "redirect:Management";
    }


    @RequestMapping(name = "根据id查询",value = "/selectById")
    @ResponseBody
    public Map<String,Object> selectById(Integer f_id){
        System.out.println(f_id+"=====aaaaa");
        Map<String,Object> map = new HashMap<>();
        map.put("byId",service.selectById(f_id));
        return  map;
    }



    @RequestMapping(name = "修改",value = "/update")
    public String update(Integer type_code, String type_abbreviate, String type_name,String f_amount, double g_day,double g_month,double g_year, Integer h_id,Integer f_id){
        System.out.println(f_id+"======aaaaa");
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = df.format(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_WEEK, h_id);
        String format1 = df.format(calendar.getTime());
        String s = format + "~" + format1;
        //添加
        int min1 = 100000; // 定义随机数的最小值
        int max1 = 999999; // 定义随机数的最大值
        // 产生一个7~107的数
        int z1 = (int) min1 + (int) (Math.random() * (max1 - min1));
        service.update(new Fund_Management(z1,type_abbreviate,type_name,g_day,g_month,g_year,f_amount,h_id+"",s,f_id));
        return "redirect:Management";
    }
    @RequestMapping(name = "删除",value = "/delete")
    @ResponseBody
    public PageInfo<Fund_Management> delete(Integer f_id){
        System.out.println(f_id+"=====aaaaa");
        boolean b = service.delete(f_id);
        PageInfo<Fund_Management> allUsersByPage = service.getAllUsersByPage(0, 5);
        return allUsersByPage;
    }
}
